package servlet2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponseIOServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
        * 字节流与字符流只能使用一个
        * 字符流多用于字符串传输
        * 字节流多用于下载
        * //字符流
        * response.getWriter();
        * //字节流
        * response.getOutputStream();
        * */

        //-----------------解决响应的中文乱码问题方法一-----------------
        /*
            //获取响应的字符集
            System.out.println(response.getCharacterEncoding());//默认IOS-8859-1
            //设置服务器字符集为UTF-8
            response.setCharacterEncoding("UTF-8");
            //通过相应头，设置浏览器也使用UTF-8字符集
            response.setHeader("Content-Type", "text/html; charset=UTF-8");
        */
        //-----------------解决响应的中文乱码问题方法二-----------------
        //它会同时设置服务器和客户端都使用UTF-8字符集，还设置了响应头
        //此方法必须要在获取流对象之前使用才有效
        response.setContentType("text/html; charset=UTF-8");

        //要求：在客户端回传 字符串数据
        PrintWriter writer = response.getWriter();
        writer.write("response's context!!!");

    }
}
